<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;
use Storage;

/**
 * Class Banner
 *
 * @package App
 * @mixin \Eloquent
 */
class Czcomment extends Model
{
    public $timestamps = false;
    protected $table = 'kl_circle_comment';


    //删除
    const DEL_NO = 0;//删除了
    const DEL_OFF = 1; //未删除

    static $DelDisplay = [
        self::DEL_NO => '不显示',
        self::DEL_OFF => '显示'
    ];

    /**
     * 新增
     * @param $request
     * @return array
     */
    static function pinglunPublish($request)
    {
        DB::beginTransaction();

        //添加资讯
        $advice = new Czcomment();
        $advice->user_id = $request->user_id;
        $advice->circle_article_id = $request->circle_article_id;
        $advice->content = $request->content;
        $advice->is_del = 1;
		
		if(DB::table('t_member')->where('c_id', $request->user_id)->value('name')){
			$advice->user_name = DB::table('t_member')->where('c_id', $request->user_id)->value('name');
		}

		if(DB::table('t_member')->where('c_id', $request->user_id)->value('c_avatar')){
			$advice->user_avatr = DB::table('t_member')->where('c_id', $request->user_id)->value('c_avatar');
		}

        $advice->create_time = date('Y-m-d H:i:s');
		
	    if(!$advice->save()) {
            DB::rollBack();
            return ['status' => 2, 'message' => '失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }
	
	//删除评论
    static function plDel($id, $request)
    {

        DB::beginTransaction();
		
        if (!DB::table('kl_circle_comment')->where('id', $id)->delete()) {
			DB::rollBack();
            return ['status' => 2, 'message' => '删除失败'];
        }
		if(DB::table('kl_circle_reply')->where('comment_id', $id)->first()){
			if (!DB::table('kl_circle_reply')->where('comment_id', $id)->delete()) {
				DB::rollBack();
				return ['status' => 2, 'message' => '删除失败'];
			}				
		}
		
        DB::commit();

        return ['status' => 1, 'message' => '删除成功'];
    }

}
